<?php
/**
 * Class to connect with the Last.FM API
 *
 * @author Antwan van der Mooren
 */

class LastFMService
{
	const API_KEY		= "e924090151ad941890bfea78c54f9ca6";
	const API_SECRET	= "46e900154a28425db596a671151e07b1";
	const API_BASE_URL	= "http://ws.audioscrobbler.com/2.0/";

	public function  __construct()
	{
		//
	}
	
	/**
	 * Get a user by his username
	 * 
	 * @param string $username
	 * @return json
	 */
	public function getUserByName($username)
	{
        $url = self::API_BASE_URL;
		$url .= "?method=user.getInfo";
		$url .= "&user=".$username;
		$url .= "&api_key=".self::API_KEY;

		$result = simplexml_load_file(urlencode($url));
		return json_encode($result);
	}

	/**
	 * Get friends of a User
	 *
	 * @param string $username
\	 * @param int $limit
	 * @return json
	 */
	public function getFriendsByUser($username, $limit)
	{
        $url = self::API_BASE_URL;
		$url .= "?method=user.getFriends";
		$url .= "&user=".$username;
		$url .= "&limit=".$limit;
		$url .= "&api_key=".self::API_KEY;

		$result = simplexml_load_file(urlencode($url));
		return json_encode($result);
	}

	/**
	 * Get the artist from the user, limit by artistCount
	 *
	 * @param string $username
	 * @param int $limit
	 * @return json
	 */
	public function getArtistsByUser($username, $limit = 10)
	{
        $url = self::API_BASE_URL;
		$url .= "?method=library.getArtists";
		$url .= "&user=".$username;
		$url .= "&limit=".$limit;
		$url .= "&api_key=".self::API_KEY;

		$result = simplexml_load_file(urlencode($url));
		return json_encode($result);
	}

	/**
	 * Get topalbums from one artist
	 *
	 * @param string $artistName
	 * @param string $id
	 * @return json
	 */
	public function getTopAlbumsByArtist($artistName, $id = "")
	{
        $url = self::API_BASE_URL;
		$url .= "?method=artist.getTopAlbums";
		$url .= "&artist=".$artistName;
		$url .= "&mbid=".$id;
		$url .= "&api_key=".self::API_KEY;

		$result = simplexml_load_file(urlencode($url));
		return json_encode($result);
	}

	/**
	 * Compare taste of two users
	 *
	 * @param string $user1
	 * @param string $user2
	 * @param int $limit
	 * @return json
	 */
	public function compareUserTaste($user1, $user2, $limit)
	{
        $url = self::API_BASE_URL;
		$url .= "?method=tasteometer.compare";
		$url .= "&type1=user";
		$url .= "&type2=user";
		$url .= "&value1=".$user1;
		$url .= "&value2=".$user2;
		$url .= "&limit=".$limit;
		$url .= "&api_key=".self::API_KEY;

		$result = simplexml_load_file(urlencode($url));
		return json_encode($result);
	}
}
?>
